#!/bin/bash
# 功能: 安装 JDK
# 名称: jdk_install.sh
# 作者: zxm
# 创建日期: 2024-11-22
# 版本: v1.0

# 设置脚本出错时立即退出
set -e

# 全局变量
FIND_STR="UINO_HOME"
DEFAULT_UINO_HOME="/uino"
JDK_ARCHIVE="jdk_linux.tar.gz"
JDK_DIR_NAME="jdk_linux"

# 检查环境变量是否已设置
check_env_var() {
        local var_name="$1" # 使用 local 定义局部变量，并正确引用参数

        # 检查是否传递了有效的参数
        if [[ -z "$var_name" ]]; then
                print_red "Error: No variable name provided to check_env_var function."
                exit 1
        fi

        # 检查环境变量是否为空
        if [[ -z "${!var_name}" ]]; then
                return 1 # 未设置
        else
                return 0 # 已设置
        fi
}

# 检查配置文件中是否包含指定字符串
check_in_file() {
        local file="$1"
        local str="$2"
        if [[ -f "$file" ]] && grep -q "$str" "$file"; then
                return 0 # 存在
        else
                return 1 # 不存在
        fi
}

# 设置环境变量到指定文件
set_env_var() {
        local file="$1"
        local key="$2"
        local value="$3"
        if ! grep -q "$key=$value" "$file"; then
                echo "$key=$value" >>"$file"
        fi
}

# 输出绿色文本的函数
print_green() {
        local text="$1"
        echo -e "\033[32m${text}\033[0m"
}

# 输出红色文本的函数
print_red() {
        local text="$1"
        echo -e "\033[31m${text}\033[0m"
}

# 设置 JDK 环境变量
set_jdk_env() {
        local profile_file="$1"

        set_env_var "$profile_file" "JAVA_HOME" "${UINO_HOME}/${JDK_DIR_NAME}"
        set_env_var "$profile_file" "JRE_HOME" "${UINO_HOME}/${JDK_DIR_NAME}/jre"

        if ! grep -q "PATH=\$JAVA_HOME/bin:\$JAVA_HOME/lib" "$profile_file"; then
                echo 'PATH=$JAVA_HOME/bin:$JAVA_HOME/lib:$PATH' >>"$profile_file"
        fi

        if ! grep -q "CLASSPATH=\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar" "$profile_file"; then
                echo 'CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' >>"$profile_file"
        fi

        if ! grep -q "export JAVA_HOME JRE_HOME PATH CLASSPATH" "$profile_file"; then
                echo "export JAVA_HOME JRE_HOME PATH CLASSPATH" >>"$profile_file"
        fi
}

# 创建目录并检查权限
create_directory() {
        local dir="$1"
        if [[ ! -d "$dir" ]]; then
                mkdir -p "$dir"
        fi
        if [[ ! -w "$dir" || ! -x "$dir" ]]; then
                print_red "Error: You do not have permission to access $dir. Please use another directory or contact the administrator."
                exit 1
        fi
}

# 安装 JDK
install_jdk() {
        echo "Decompressing JDK to ${UINO_HOME}..."
        tar zxf "$JDK_ARCHIVE" -C "$UINO_HOME"

        ## 测试 JDK 的版本
        ${DEFAULT_UINO_HOME}/${JDK_DIR_NAME}/bin/java -version

        if [[ "$USER" == "root" ]]; then
                set_jdk_env /etc/profile
                source /etc/profile
                print_green "Installation complete. Please execute \"source /etc/profile\" to apply changes!"
        else
                set_jdk_env ~/.bashrc
                source ~/.bashrc
                print_green "Installation complete. Please execute \"source ~/.bashrc\" to apply changes!"
        fi
}

# 主逻辑
main() {
        # 检查 UINO_HOME 是否已设置
        if ! check_env_var "$FIND_STR"; then
                echo "$FIND_STR is not set. Please input a directory to install (default: ${DEFAULT_UINO_HOME}):"
                read -r UINO_INPUT
                UINO_HOME=${UINO_INPUT:-$DEFAULT_UINO_HOME}
        fi

        # 检查配置文件中是否已设置 UINO_HOME
        if ! check_in_file /etc/profile "$FIND_STR" &&
                ! check_in_file ~/.bashrc "$FIND_STR" &&
                ! check_in_file ~/.bash_profile "$FIND_STR"; then
                create_directory "$UINO_HOME"
                source ~/.bashrc
                echo "UINO_HOME has been set to ${UINO_HOME} and saved in ~/.bashrc"
        else
                echo "UINO_HOME is already set to ${UINO_HOME}"
        fi

        # 创建 UINO_HOME 目录
        create_directory "$UINO_HOME"

        # 安装 JDK
        install_jdk
}

# 执行主逻辑
main
